package com.java.yz.day10.ketang;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

//存储字符串并遍历
public class CollectionText1 {
    public static void main(String[] args) {
        //创建集合对象
        Collection c1  = new ArrayList();

        //创建字符串对象并加到集合中
        c1.add("hello");
        c1.add("hadoop");
        c1.add("hive");
        c1.add("java");

        //遍历数组
        //1.转数组遍历
        Object[] o = c1.toArray();
        for (int i = 0; i <o.length ; i++) {
            String s = (String)o[i];
            System.out.println(o[i]);
        }

        //2.迭代器遍历
        Iterator iterator = c1.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
            //向下转型   可以获取String的属性
            String s= (String) next;
            System.out.println(s.length());
        }

    }
}
